package com.lineage.server.model.weaponskill;

import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Magic;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class W_SK009 extends L1WeaponSkillType {
  private static final Log _log = LogFactory.getLog(W_SK009.class);
  
  private static final Random _random = new Random();
  
  public static L1WeaponSkillType get() {
    return new W_SK009();
  }
  
  public double start_weapon_skill(L1PcInstance pc, L1Character target, L1ItemInstance weapon, double srcdmg) {
    try {
      int chance = _random.nextInt(1000);
      if (random(weapon) >= chance) {
        if (target.hasSkillEffect(31)) {
          target.removeSkillEffect(31);
          int castgfx2 = SkillsTable.get().getTemplate(31).getCastGfx2();
          target.broadcastPacketAll((ServerBasePacket)new S_SkillSound(target.getId(), castgfx2));
          if (target instanceof L1PcInstance) {
            L1PcInstance tgpc = (L1PcInstance)target;
            tgpc.sendPacketsAll((ServerBasePacket)new S_SkillSound(tgpc.getId(), castgfx2));
          } 
          return 0.0D;
        } 
        if (target.hasSkillEffect(153))
          target.removeSkillEffect(153); 
        if (target.getCurrentMp() > 0) {
          int mana = this._type1 + _random.nextInt(this._type2);
          int drainMana = mana + pc.getInt() / 2;
          if (target.getCurrentMp() < drainMana)
            drainMana = target.getCurrentMp(); 
          L1Magic magic = new L1Magic((L1Character)pc, target);
          magic.commit(0, drainMana);
          show(pc, target);
        } 
      } 
      return 0.0D;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return 0.0D;
    } 
  }
}
